package com.ming.demo.easyiot;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.litepal.crud.DataSupport;

import java.util.List;

import static android.R.attr.id;
import static com.amap.api.mapcore2d.p.i;

/**
 * Created by Administrator on 2018/1/8.
 */

public class HistoryActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView mRecyclerView;
    private TextView mTvRight;
    private HistoryAdapter mAdapter;
    List<ServiceInfo> list;
    private LinearLayout mLyDelete, mLyReport;
    private CheckBox mCheckBoxAll;
    private int isSelectAll = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        mRecyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        list = DataSupport.findAll(ServiceInfo.class);
        while (list.size() > 100) {
            int id = list.get(0).getId();
            list.remove(0);
            DataSupport.delete(ServiceInfo.class, id);
        }
        mAdapter = new HistoryAdapter(this, list);
        mRecyclerView.setAdapter(mAdapter);
        mTvRight = (TextView) findViewById(R.id.mTvRight);
        mTvRight.setOnClickListener(this);
        mLyDelete = (LinearLayout) findViewById(R.id.mLyDelete);
        mLyReport = (LinearLayout) findViewById(R.id.mLyReport);
        mCheckBoxAll = (CheckBox) findViewById(R.id.mCheckBoxAll);
        mLyDelete.setOnClickListener(this);
        mLyReport.setOnClickListener(this);
        mCheckBoxAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mAdapter.selectAll(isSelectAll);
                if (b) {
                    isSelectAll = 1;
                } else {
                    isSelectAll = 0;
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mTvRight:
                finish();
                break;
            case R.id.mLyDelete:
                mAdapter.delete();
                break;
            case R.id.mLyReport:
                mAdapter.update();
                break;
        }
    }
}
